package Events;

import java.awt.Color;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URLClassLoader;
import java.util.Random;
import java.util.Scanner;

import Announcements.Announcement;
import Main.SpaceGame;
import World.TileType;
import World.World;

public class PirateAttack implements Event
{
	static int probibility = 170;//000;
	private static final Random rand = new Random();
	private int ship = 0;
	private int x,y,shipWidth,shipHeight;
	private boolean done = false;
	
	@Override
	public void update(SpaceGame main) 
	{
		
	}

	@Override
	public void init(SpaceGame main) 
	{
		int loopCnt = 0;
		//ship = rand.nextInt(6);
		ship = 5;
		Scanner input = null;
		
		input = new Scanner(URLClassLoader.getSystemResourceAsStream("Resources/Ship"+ship+".txt"));
		
		shipWidth = input.nextInt();
		shipHeight = input.nextInt();
		
		x = World.worldXSize - shipWidth-1-rand.nextInt(5);
		y = rand.nextInt(World.worldYSize - shipHeight-10)+10;
		
		while(checkArea(x,y, main) != true)
		{
			y = rand.nextInt(World.worldYSize - shipHeight);
			loopCnt++;
			if(loopCnt > 30)
			{
				done = true;
				break;
			}
		}
		if (!done)
		{
			World.spawn(x, y, "Resources/Ship"+ship+".txt", main);
			main.announcementManager.AddAnnouncement(new Announcement("A hostile ship is within range!", Color.red));
		}
		done = true;
	}

	@Override
	public boolean isDone() 
	{
		return done;
	}
	
	public boolean checkArea(int x, int y,SpaceGame main)
	{
		boolean clear = true;
		for(int dx = x;dx<= shipWidth+x;dx++)
		{
			for(int dy = y;dy<=shipHeight+y;dy++)
			{
				if(main.world.tiles[dx][dy].getType() != TileType.Space)
				{
					clear = false;
				}
			}
		}
		return clear;
	}
}
